#!/bin/bash

# A script that builds and runs the CMake project
#   - create build directory
#   - cd to build directory, run cmake and make
#   - run the specified command passed in as commandline parameters

# Create build directory and build project
if [[ -d $build_dir ]]; then
    rm -rf build
fi

if [ -f "/home/pi/miniconda3/bin/activate" ]; then
  export PATH="/home/pi/miniconda3/bin:$PATH"
  source activate py34 > /dev/null 2>&1
fi

mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -s

# export OPENBLAS_NUM_THREADS=1

# Dump list of processes currently running (to check for runaway tests)
/bin/bash -c "ps -e -o pid,%cpu,comm k -%cpu | tee ../processes.txt"

# Run profile tool with arguments given on the command line
if [[ -f ./profile ]] ; then
  echo "Running ELL profiler tool -- no opt" | tee -a ../profile_report_noopt.txt
  /bin/bash -c "LD_PRELOAD=/home/pi/miniconda3/envs/py34/lib/libopenblas.so ./profile 100 20 | tee -a ../profile_report_noopt.txt"
fi

if [[ -f ./profile_opt ]] ; then
  echo "Running ELL profiler tool -- opt" | tee -a ../profile_report_opt.txt
  /bin/bash -c "LD_PRELOAD=/home/pi/miniconda3/envs/py34/lib/libopenblas.so ./profile_opt 100 20 | tee -a ../profile_report_opt.txt"
fi

# Run exercise_model tool with 10 iterations
if [[ -f ./exercise_model ]] ; then
  echo "Timing ELL exercise tool -- no opt" | tee -a ../profile_report_noopt.txt
  /bin/bash -c "LD_PRELOAD=/home/pi/miniconda3/envs/py34/lib/libopenblas.so time ./exercise_model 100" 2>&1 | tee -a ../profile_report_noopt.txt
fi

if [[ -f ./exercise_model_opt ]] ; then
  echo "Timing ELL exercise tool -- opt" | tee -a ../profile_report_opt.txt
  /bin/bash -c "LD_PRELOAD=/home/pi/miniconda3/envs/py34/lib/libopenblas.so time ./exercise_model_opt 100" 2>&1 | tee -a ../profile_report_opt.txt
fi

# Run perf tool to get low-level events
#
# To install perf:
#
# uname -a  # get Linux version
#
# sudo apt-get install linux-perf-4.9  # (Use this for Raspbian Jesse)
#   --or--
# sudo apt-get install linux-tools-3.6  # for older versions of Linux (?)

# perf events to try looking at:
#
#   cpu-time
#   instructions
#   cpu-cycles
#   cache-references
#   cache-misses
#   LLC-loads
#   LLC-load-misses
#   dTLB-load-misses
#   branches
#   branch-misses
#
# other:
#   faults
#   L1-dcache-load-misses
#   context-switches (cs)
#   cpu-migrations

# Note: On the ARM, we can only collect samples from 4 counters at a time
# Note: add '-s' option to perf record and '-T' option to perf report to get per-thread info

# Run perf tool to get overall stats
# echo "Getting overall perf stats -- normal"
# /bin/bash -c "perf stat -e cpu-cycles,instructions,cache-misses ./exercise_model 100"

# echo "Getting overall perf stats -- opt"
# /bin/bash -c "perf stat -e cpu-cycles,instructions,cache-misses ./exercise_model_opt 100"

# Run perf tool to record profile data
# echo "Recording perf data"
# Note: Diverting output to /dev/null because we keep getting "Exception: <class 'UnicodeDecodeError'>: 'utf-8' codec can't decode byte 0x98 in position 22: invalid start byte"
#       And also because the SSH client used in RemoteRunner was freezing.
# /bin/bash -c "perf record -e cpu-clock,faults -q ./exercise_model 10 > /dev/null"

# Report 1
# echo "Perf profile data report 1"
# /bin/bash -c "perf report --stdio --stdio-color never --sort comm,dso > report1.txt"
# /bin/bash -c "perf report --stdio --sort comm,dso"

# Report 2
# echo "perf profile data report 2"
# perf report --stdio --dsos=exercise_model,libc > report2.txt
